package com.example.farmeri.presenter.login;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.example.farmeri.R;
import com.example.farmeri.http.API;
import com.example.farmeri.data.DataParam;
import com.example.farmeri.data.RetrofitManager;
import com.example.farmeri.pojo.User;
import com.example.farmeri.presenter.base.BasePresenter;
import com.example.farmeri.tools.ResponseUtils;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.FormBody;
import okhttp3.RequestBody;

public class LoginPresenter extends BasePresenter<ILoginContract> {


    public LoginPresenter(ILoginContract view) {
        super(view);
    }


    public void login(User user){
        RequestBody requestBody = new FormBody.Builder()
                .add("login_name",user.getLogin_name())
                .add("tel",user.getTel())
                .add("age",user.getAge())
                .add("password",user.getPassword())
                .build();
        RetrofitManager
                .getManager()
                .getRetrofit()
                .create(API.class)
                .login(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils<User>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils<User> response) {
                        getView().onLoginSuccess(response);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        getView().showMessage(null);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void register(User registerUser) {
        //设置提交的请求
        RequestBody requestBody = new FormBody.Builder()
                .add("loginName", registerUser.getLogin_name())
                .add("tel", registerUser.getTel())
                .add("email", registerUser.getEmail())
                .add("identity", registerUser.getIdentity())
                .add("requestPlace", registerUser.getRequest_place())
                .add("age", registerUser.getAge())
                //.add("code", registerModel.getCode())
                .build();

        RetrofitManager
                .getManager()
                .getRetrofit()
                .create(API.class)
                .login(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils<User>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils<User> response) {
                        getView().onLoginSuccess(response);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e(TAG, "onError: "+e);
                        getView().showMessage("登录错误");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    public void saveUserInfo(Context context, User user) {
        DataParam.setUserInfo(user);
        saveUserInfoInfile(context,user);
    }

    private void saveUserInfoInfile(Context context, User user) {
        SharedPreferences sharedPre = context.getSharedPreferences(context.getString(R.string.share_preference_user_info),Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPre.edit();
        editor.putString(context.getResources().getString(R.string.user_info_key_id) ,user.getId());
        editor.putString(context.getResources().getString(R.string.user_info_key_age),user.getAge());
        editor.putFloat(context.getResources().getString(R.string.user_info_key_credit),user.getCredit());
        editor.putString(context.getResources().getString(R.string.user_info_key_email),user.getEmail());
        editor.putString(context.getResources().getString(R.string.user_info_key_icon),user.getIcon());
        editor.putString(context.getResources().getString(R.string.user_info_key_identity),user.getIdentity());
        editor.putInt(context.getResources().getString(R.string.user_info_key_isAdmin),user.getIsAdmin());
        editor.putString(context.getResources().getString(R.string.user_info_key_loginName),user.getLogin_name());
        editor.putString(context.getResources().getString(R.string.user_info_key_nickName),user.getNick_name());
        editor.putString(context.getResources().getString(R.string.user_info_key_tel),user.getTel());
        editor.putString(context.getResources().getString(R.string.user_info_key_remark),user.getRemarks());
        //editor.apply();
        Log.d("sharePre", "saveUserInfo: commit" + editor.commit());

    }


    /**
     * 监测是否有内容未输入
     * @return bool
     */
    public boolean checkData(Context context, List<EditText> formList) {
        String content = null;
        //判断输入内容是否为空

        for (EditText editText:formList){
            content = editText.getText().toString();
            if (TextUtils.isEmpty(content)){
                editText.setFocusable(true);
                Toast.makeText(context,"请将信息输入完整",Toast.LENGTH_SHORT).show();
                return false;
            }
        }
        return true;
    }
}
